package cn.fresh.xuanyuan.module.system.service.wx;

import cn.fresh.xuanyuan.framework.common.pojo.PageParam;
import cn.fresh.xuanyuan.framework.common.pojo.PageResult;
import cn.fresh.xuanyuan.module.system.dal.dataobject.wx.TemplateConfigDO;
import cn.fresh.xuanyuan.module.system.dal.dataobject.wx.WXTemplateDataDO;
import cn.fresh.xuanyuan.module.system.dal.mysql.wx.TemplateConfigMapper;
import cn.fresh.xuanyuan.module.system.model.form.AddWXTemplateConfigForm;
import cn.fresh.xuanyuan.module.system.model.request.WXTemplateDataMap;
import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class WXTemplateConfigServiceImpl implements WXTemplateConfigService {

    @Resource
    private TemplateConfigMapper templateConfigMapper;

    @Override
    public void addTemplateConfig(Long sysUserId, AddWXTemplateConfigForm form) {
        List<WXTemplateDataDO> list = checkDataMap(form.getData());
        if(CollectionUtil.isEmpty(list)) {
            return;
        }
        TemplateConfigDO configDO = new TemplateConfigDO();
        BeanUtils.copyProperties(form, configDO);
        configDO.setDataMap(list);
        configDO.setCreator(String.valueOf(sysUserId));
        configDO.setCreateTime(LocalDateTime.now());
        templateConfigMapper.insert(configDO);
    }

    @Override
    public void editTemplateConfig(Long sysUserId, AddWXTemplateConfigForm form) {
        List<WXTemplateDataDO> list = checkDataMap(form.getData());
        if(CollectionUtil.isEmpty(list)) {
            return;
        }
        TemplateConfigDO configDO = new TemplateConfigDO();
        BeanUtils.copyProperties(form, configDO);
        configDO.setDataMap(list);
        configDO.setUpdater(String.valueOf(sysUserId));
        configDO.setUpdateTime(LocalDateTime.now());
        templateConfigMapper.updateById(configDO);
    }

    private List<WXTemplateDataDO> checkDataMap(List<WXTemplateDataMap> list) {
        List<WXTemplateDataDO> result = new ArrayList<>();
        for(WXTemplateDataMap map : list) {
            if(StringUtils.isNotBlank(map.getDataKey())) {
                WXTemplateDataDO data = new WXTemplateDataDO();
                data.setDataKey(map.getDataKey());
                data.setValue(map.getValue());
                data.setTitle(map.getTitle());
                result.add(data);
            }
        }
        return result;
    }

    @Override
    public PageResult<TemplateConfigDO> getPage(int pageNo, int pageSize) {
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(pageNo);
        pageParam.setPageSize(pageSize);
        PageResult<TemplateConfigDO> page = templateConfigMapper.getPage(pageParam);
        return page;
    }
}
